package Ventanas;

import Objetos.PropiedadesIP;
import Proxies.CtrlCrearGrupoWS;
import Proxies.CtrlCrearGrupoWSService;
import Proxies.CtrlUsuariosWS;
import Proxies.CtrlUsuariosWSService;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.xml.namespace.QName;

/**
 *
 * @author Mathias
 */
public class BloqueoUsuario extends javax.swing.JFrame {
    
    
    CtrlUsuariosWS amista;
    CtrlCrearGrupoWS amista2;
    String nomUsuario;
    public BloqueoUsuario() {
        initComponents();
        
        PropiedadesIP props = PropiedadesIP.getInstance();
        String IP = props.getServidorCentralIP();
        
        URL dirGr = null;
        try {
            dirGr = new URL(IP + "CtrlCrearGrupo?wsdl");
        } catch (MalformedURLException ex) {
            Logger.getLogger(BloqueoUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
        QName qNaGr = new QName("http://WebServices/", "CtrlCrearGrupoWSService");
        amista2 = new CtrlCrearGrupoWSService(dirGr, qNaGr).getCtrlCrearGrupoWSPort();
        
        URL dirUsr = null;
        try {
            dirUsr = new URL(IP + "CtrlUsuarios?wsdl");
        } catch (MalformedURLException ex) {
            Logger.getLogger(BloqueoUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }
        QName qNaUsr = new QName("http://WebServices/", "CtrlUsuariosWSService");
        amista = new CtrlUsuariosWSService(dirUsr, qNaUsr).getCtrlUsuariosWSPort();
        
        Object [] fila = new Object[3];
        Iterator<Proxies.DataUsuario> itera = amista.listaUsuarios().getItem().iterator();
        Proxies.DataUsuario usr;
        DefaultTableModel model = (DefaultTableModel) Table_buscarUsr.getModel();
        while (itera.hasNext()){
            usr = itera.next();
            fila[0] = usr.getNombre();
            fila[1] = usr.getNick();
            fila[2] = usr.getCorreo();
            model.addRow(fila);
        }
    
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TextField_BuscarUsuario = new javax.swing.JTextField();
        jScrollPane4 = new javax.swing.JScrollPane();
        Table_buscarUsr = new javax.swing.JTable();
        vBuscar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();

        TextField_BuscarUsuario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                TextField_BuscarUsuarioKeyReleased(evt);
            }
        });

        Table_buscarUsr.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Nick", "Correo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane4.setViewportView(Table_buscarUsr);

        vBuscar.setText("Selecinar");
        vBuscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                vBuscarMouseClicked(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Usuario : ");

        jLabel11.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel11.setText("RED - Bloquear usuarios");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel11))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(80, 80, 80)
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(TextField_BuscarUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(vBuscar)
                        .addGap(40, 40, 40)))
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(55, 55, 55)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(TextField_BuscarUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel3))))
                        .addGap(18, 18, 18)
                        .addComponent(vBuscar)
                        .addGap(1, 1, 1)))
                .addContainerGap(172, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void TextField_BuscarUsuarioKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TextField_BuscarUsuarioKeyReleased
// TODO add your handling code here:
        
    
        TableModel modelo = Table_buscarUsr.getModel();
        final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(modelo);
        Table_buscarUsr.setRowSorter(sorter);
        String text = TextField_BuscarUsuario.getText();
        if (text.length() == 0) {
          sorter.setRowFilter(null);
        } else {
          sorter.setRowFilter(RowFilter.regexFilter(text));
          
        } 
}//GEN-LAST:event_TextField_BuscarUsuarioKeyReleased

private void vBuscarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_vBuscarMouseClicked
// TODO add your handling code here:
    //boolean es = amista.esCreadorGrupo(nomUsuario);
    if(Table_buscarUsr.getSelectedRow()==-1)
        nomUsuario = Table_buscarUsr.getValueAt(0, 2).toString();
    else 
        nomUsuario = Table_buscarUsr.getValueAt(Table_buscarUsr.getSelectedRow(),2).toString();
        Proxies.DataUsuario usr = amista.getUsuario(nomUsuario);

    if (amista.esCreadorGrupo(nomUsuario)) {
        int result = JOptionPane.showConfirmDialog(null, "Si bloquea este usuario se daran de baja todos los grupos que haya creado. Desea confirmar el bloqueo?");
        if (JOptionPane.OK_OPTION == result) {
//            try {
                amista.bloquearUsuario(usr);
                    Proxies.DataUsuario de = amista.getUsuario(nomUsuario);
                JOptionPane.showMessageDialog(null, "Se ha bloqueado al usuario " + de.getNick());
//            } catch (IOException ex) {
//                JOptionPane.showMessageDialog(null, "No se pudo bloquear al usuario " + usr.getNick());
//            }
        }else {
            JOptionPane.showMessageDialog(null, "No se ha bloqueado al usuario " + usr.getNick());
        }
    } else {
//        try {
            amista.bloquearUsuario(usr);
            JOptionPane.showMessageDialog(null, "Se ha bloqueado al usuario " + usr.getNick());
//        } catch (IOException ex) {
//            JOptionPane.showMessageDialog(null, "No se pudo bloquear al usuario " + usr.getNick());
//        }
    }
}//GEN-LAST:event_vBuscarMouseClicked
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BloqueoUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BloqueoUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BloqueoUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BloqueoUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new BloqueoUsuario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable Table_buscarUsr;
    private javax.swing.JTextField TextField_BuscarUsuario;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JButton vBuscar;
    // End of variables declaration//GEN-END:variables
}
